package com.bw.finance.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.finance.conf.ResponseResult;
import com.bw.finance.eneity.cms.EduWebsiteAd;
import com.bw.finance.eneity.cms.EduWebsiteBase;
import com.bw.finance.service.IEduWebsiteAdService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zxx
 * @since 2020-08-16
 */
@RestController
@RequestMapping("/wbad")
public class EduWebsiteAdController {

    @Autowired
    private IEduWebsiteAdService adService;

    private static final Logger log= LoggerFactory.getLogger(EduWebsiteAdController.class);

    @PostMapping("/adlist")
    public ResponseResult adlist(@RequestBody EduWebsiteAd websiteAd){
        log.info("网站广告信息查询开始");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper<EduWebsiteAd> queryWrapper=new QueryWrapper();
            if(websiteAd.getAdName()!=null){
                queryWrapper.lambda().eq(EduWebsiteAd::getAdName,websiteAd.getAdName());
            }
            List<EduWebsiteAd> list = adService.list(queryWrapper);
            result.setResult(list);
        }catch (Exception e){
            log.error("网站广告信息查询异常"+e.toString());
            result.setSuccess(false);
            result.setMessage("网站广告信息查询异常"+e.toString());
        }
        log.info("网站广告信息查询结束");
        return result;
    }

    @GetMapping("/updatestatus/{id}/{status}")
    public ResponseResult updatestatus(@PathVariable Integer id,@PathVariable Integer status){
        log.info("网站广告状态修改开始");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            EduWebsiteAd websiteAd = adService.getById(id);
            if(status==0){
                websiteAd.setStatus(1);
            }
            if(status==1){
                websiteAd.setStatus(0);
            }
            adService.saveOrUpdate(websiteAd);
            result.setMessage("修改成功！！！");
        }catch (Exception e){
            log.error("网站广告状态修改异常"+e.toString());
            result.setSuccess(false);
            result.setMessage("网站广告状态修改异常"+e.toString());
        }
        log.info("网站广告状态修改结束");
        return result;
    }

    @PostMapping("/addwbad")
    public ResponseResult findwbbase(@RequestBody EduWebsiteAd websiteAd){
        log.info("网站基本信息保存开始");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            adService.saveOrUpdate(websiteAd);
            result.setMessage("保存成功！！！");
        }catch (Exception e){
            log.error("网站基本信息保存异常"+e.toString());
            result.setSuccess(false);
            result.setMessage("网站基本信息修改异常"+e.toString());
        }
        log.info("网站基本信息保存结束");
        return result;
    }

    @GetMapping("/get")
    public List<EduWebsiteAd> get(){
        log.info("轮播图数据获取开始");
        ResponseResult result = ResponseResult.SUCCESS();
        List<EduWebsiteAd> list=new ArrayList<>();
        try {
            QueryWrapper<EduWebsiteAd> queryWrapper=new QueryWrapper();
            queryWrapper.lambda().eq(EduWebsiteAd::getAdType,0);
            list = adService.list(queryWrapper);
        }catch (Exception e){
            log.error("轮播图数据获取异常"+e.toString());
            result.setSuccess(false);
            result.setMessage("轮播图数据获取异常"+e.toString());
        }
        log.info("轮播图数据获取结束");
        return list;
    }
}
